#-*- coding: utf-8 -*

# 此程序为读取rfid串口设备的卡号

from Myserial import Myserial
from PyQt4 import QtCore

class RFIDRead(QtCore.QThread):
    '''
        This class is defined as a RFIDreader whcih can get the card id from serial rfid-device
    '''
    IDSignal = QtCore.pyqtSignal(str)

    def __init__(self, serial_baud=9600, parent=None):
        super(RFIDRead, self).__init__(parent)

        self.serial_baud = serial_baud
        self.serial_port = '/dev/RFID'
        self.rfid = Myserial(self.serial_baud,self.serial_port)
        # clear the buf of receive FIFO
        self.rfid.current_serial_port.flushInput()
    
    def run(self):
        self.rfid.current_serial_port.flushInput()
        while True:
            self.IDSignal.emit(self.get_card_id()) # send the singal of rfid_id
    
    def get_card_id(self):
        self.card_id = ''
        # get the card id list 
        card_bufs = (((self.rfid).get_RxData_str())[1])[7:11]
        # get the right order of the card id list
        card_bufs.reverse()
        # get the final string of card id
        for card_buf in card_bufs:
            self.card_id += card_buf
        # clean the card_buf
        card_bufs = []
        return self.card_id


# rfid1 = RFIDRead()

# print("卡号为：")
# print(rfid1.get_card_id())

    